/// FileName ad_source_generator
/// @Author zhuqingfang
/// @Date 2022/9/29 3:45 下午
/// @Description ad source generator
import 'package:analyzer/dart/element/element.dart';
import 'package:source_gen/source_gen.dart';
import 'package:build/build.dart';

import 'ad_source_service.dart';

generateImport(String adSource){
  List<String> adSourceList = adSource.split(',');
  var codeString = '';
  for(var adSource in adSourceList){
    var adSourceImport = adSource;
    if(adSource == 'topOn') {
      adSourceImport = "top_on";
    }
    codeString += '''
      import 'package:${adSourceImport}_ad_plugin/${adSource}_ad_manager.dart';
      ''';
  }
  return codeString;
}
generateCode(String adSource){
  List<String> adSourceList = adSource.split(',');
  var codeString = '';
  for(var adSource in adSourceList){
    codeString += '''
      ${adSource}AdInstanceInit();
      ''';
  }
  return codeString;
}
class AdSourceServiceGenerator extends GeneratorForAnnotation<AdSourceService> {
  @override
  generateForAnnotatedElement(Element element, ConstantReader annotation, BuildStep buildStep) {
    // String className = element.displayName;
    // String path = buildStep.inputId.path;
    // String name = annotation.peek('name').stringValue;
    // $path  $name   $className
    String adSource = annotation.read("adSource").stringValue;
    // return generateCode(adSource);
    return '''
      ${generateImport(adSource)}
      
      void initAdInstance(){
          ${generateCode(adSource)}
      }
    ''';
  }
}